package proyecto1.Manager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import proyecto1.GestionDB;
import proyecto1.TO.PagoTO;

public class PagoMannager 
{
    GestionDB cnn= new GestionDB();
    
    public List<PagoTO> listar() throws Exception
    {    
        ArrayList data=new ArrayList();
        String sql = "SELECT * FROM pago";
        
        cnn.conectar();
        ResultSet rs=cnn.ejecutarConsulta(sql);
        while(rs.next())
        {
            PagoTO t = new PagoTO();
            
            t.setID_pago(rs.getString("ID_pago"));
            t.setTipo_pago(rs.getString("Tipo_pago"));
            t.setMonto_pago(rs.getString("Monto_pago"));
            t.setFecha_pago(rs.getString("Fecha_pago"));
            t.setDetalle_pago(rs.getString("Detalle_pago"));
        }
        rs.close();
        cnn.desconectar();
        return data;
    }
    
    public void Agregar(PagoTO data) throws Exception {

        String sql = "INSERT INTO pago(ID_pago,Tipo_pago,Monto_pago,Fecha_pago,Detalle_pago) VALUES (?,?,?,?,?)";
        Connection c = null;
        try {
            c = cnn.conectar();
            c.setAutoCommit(false);
            PreparedStatement ps = c.prepareStatement(sql);
            ps.setString(1, data.getID_pago());
            ps.setString(2, data.getTipo_pago());
            ps.setString(3, data.getMonto_pago());
            ps.setString(4, data.getFecha_pago());
            ps.setString(5, data.getDetalle_pago());
            ps.executeUpdate();
            c.commit();
            ps.close();

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
            c.rollback();
        } finally {
            cnn.desconectar();
        }
    }
    
    public PagoTO buscaPorid(Object id) throws Exception {
        PagoTO t = null;
        String sql = "SELECT * FROM pago WHERE ID_pago=" + id;
        //id_persona,cod_pe,nom_pe,dni_pe,dir_pe,cel_pe,ema_pe,fecnac_pe
        try {
            cnn.conectar();
            ResultSet rs = cnn.ejecutarConsulta(sql);
            if (rs.next()) {
                t = new PagoTO();
                t.setID_pago(rs.getString("ID_pago"));
                t.setTipo_pago(rs.getString("Tipo_pago"));
                t.setMonto_pago(rs.getString("Monto_pago"));
                t.setFecha_pago(rs.getString("Fecha_pago"));
                t.setDetalle_pago(rs.getString("Detalle_pago"));
                
            }
            rs.close();
            cnn.desconectar();

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        return t;
    }
    
    public void Editar(PagoTO data) throws Exception {
        String sql = "UPDATE pago SET Tipo_pago=?,Monto_pago=?,Fecha_pago=?,Detalle_pago=? WHERE ID_pago=?";
        Connection c = null;
        //id_persona,cod_pe,nom_pe,dni_pe,dir_pe,cel_pe,ema_pe,fecnac_pe
        try {
            c = cnn.conectar();
            c.setAutoCommit(false);
            try (PreparedStatement ps = c.prepareStatement(sql)) {
                ps.setString(5, data.getID_pago());
                ps.setString(1, data.getTipo_pago());
                ps.setString(2, data.getMonto_pago());
                ps.setString(3, data.getFecha_pago());
                ps.setString(4, data.getDetalle_pago());
                ps.executeUpdate();
                c.commit();
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
            c.rollback();
        } finally {
            cnn.desconectar();
        }

    }
    
    public void eliminar(String id) throws Exception {
        try {
            Connection c = cnn.conectar();
            String sql = "DELETE FROM pago WHERE ID_pago=?";
            PreparedStatement ps = c.prepareStatement(sql);
            ps.setString(1, id);
            ps.executeUpdate();
            ps.close();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        } finally {
            cnn.desconectar();
        }

    }
    
    public List<PagoTO> buscaPorDescripcion(Object des)throws Exception
    {
        ArrayList data = new ArrayList();
        PagoTO t=null;
   String sql="SELECT * FROM pago WHERE Tipo_pago LIKE '"+des+"%'";
       try {
       cnn.conectar();
       ResultSet rs=cnn.ejecutarConsulta(sql);
       while(rs.next()){
       t=new PagoTO();
            t.setID_pago(rs.getString("ID_pago"));
            t.setTipo_pago(rs.getString("Tipo_pago"));
            t.setMonto_pago(rs.getString("Monto_pago"));
            t.setFecha_pago(rs.getString("Fecha_pago"));
            t.setDetalle_pago(rs.getString("Detalle_pago"));
            data.add(t);
       }
           rs.close();
           cnn.desconectar();
           
       } catch (Exception e) {
       JOptionPane.showMessageDialog(null, e.getMessage());
       }
   
   return data;
   }
}